package com.boot.web.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.boot.web.dao.BlindBoxDao;
import com.boot.web.dao.BuyBlindBoxDao;
import com.boot.web.dao.CollageListDao;
import com.boot.web.entity.BlindBox;
import com.boot.web.entity.BuyBlindBox;
import com.boot.web.entity.CollageList;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.boot.communal.paging.InitPaging;
import com.boot.communal.response.ResponseServer;
import com.boot.web.entity.CollageMode;
import com.boot.web.service.CollageModeService;

/**
 * 拼团模式控制器
 * @author 【zhouJie】
 * @date 2021-06-04 14:22:50
 */
@RestController
@RequestMapping("/collageMode")
public class CollageModeController {

    @Autowired
    private CollageModeService collageModeService;

    @Autowired
    private BlindBoxDao blindBoxDao;

    @Autowired
    private BuyBlindBoxDao buyBlindBoxDao;

    @Autowired
    private CollageListDao collageListDao;

    @PostMapping("/save")
    public ResponseServer save(CollageMode collageMode) {
        if(collageMode.getPeopleQuantity() == null){
            return ResponseServer.error("拼团人数不正确");
        }
        collageMode.setUpdateTime(new Date());
        int row = collageModeService.save(collageMode);
        if(row > 0){
            for(int i = 0; i < collageMode.getPeopleQuantity(); i++){
                BlindBox box = new BlindBox();
                box.setQuantity(1);
                box.setCollageModeId(collageMode.getId());
                blindBoxDao.save(box);
            }
        }
        return ResponseServer.success("添加成功");
    }

    @PutMapping("/update")
    public ResponseServer update(CollageMode collageMode) {
        if("0".equals(collageMode.getIsShelf())){
            if("1".equals(collageMode.getType())){
                // 下架当前盲盒
                CollageList collageList = collageListDao.queryByModeId(collageMode.getId());
                if(collageList == null){
                    return ResponseServer.error("没有正在进行中的盲盒");
                }
                collageList.setStatus("1");
                collageListDao.update(collageList);
                List<BuyBlindBox> buyBlindBoxList = buyBlindBoxDao.queryBuyBlindBoxByUserId1(collageList.getId());
                if(buyBlindBoxList.size() > 0){
                    for(BuyBlindBox buy : buyBlindBoxList){
                        buy.setUserId(1198);
                        buy.setUpdateTime(new Date());
                        buyBlindBoxDao.update(buy);
                    }
                }
            }
        }else{
            if("1".equals(collageMode.getType())){
                CollageList collageList = collageListDao.queryByModeId_1(collageMode.getId());
                if(collageList == null){
                    return ResponseServer.error("没有正在进行中的盲盒");
                }
                List<BuyBlindBox> buyBlindBoxList = buyBlindBoxDao.queryBuyBlindBoxByUserId1_1(collageList.getId());
                if(buyBlindBoxList.size() > 0){
                    for(BuyBlindBox buy : buyBlindBoxList){
                        buy.setUserId(null);
                        buy.setUpdateTime(new Date());
                        buyBlindBoxDao.update(buy);
                    }
                    collageList.setStatus("0");
                    collageListDao.update(collageList);
                }
                CollageMode cm = collageModeService.getById(collageMode.getId());
                if(collageMode.getPeopleQuantity() == null){
                    return ResponseServer.error("拼团人数不正确");
                }
                if(cm.getPeopleQuantity() != collageMode.getPeopleQuantity()){
                    List<BuyBlindBox> list = buyBlindBoxDao.queryBuyBlindBoxByUserId(collageMode.getId());
                    if(list.size() > 0){
                        return ResponseServer.error("当前模式正在进行中，不可修改人数");
                    }else{
                        blindBoxDao.deleteByModeId(collageMode.getId());
                        buyBlindBoxDao.deleteByModeId(collageMode.getId());
                        for(int i = 0; i < collageMode.getPeopleQuantity(); i++){
                            BlindBox box = new BlindBox();
                            box.setQuantity(1);
                            box.setCollageModeId(collageMode.getId());
                            blindBoxDao.save(box);
                        }
                    }
                }
            }
        }
        collageMode.setUpdateTime(new Date());
        collageModeService.update(collageMode);
        return ResponseServer.success("修改成功");
    }

    @DeleteMapping("/delete/{id}")
    public ResponseServer delete(@PathVariable Integer id) {
        blindBoxDao.deleteByModeId(id);
        buyBlindBoxDao.deleteByModeId(id);
        collageModeService.delete(id);
        return ResponseServer.success("删除成功");
    }

    @DeleteMapping("/batchDelete/{ids}")
    public ResponseServer batchDelete(@PathVariable Integer[] ids) {
        return ResponseServer.success("批量删除成功", collageModeService.batchDelete(ids));
    }

    @GetMapping("/getPageList")
    public ResponseServer getPageList(@RequestParam Map<String, Object> params) {
        InitPaging.init(params);
        return ResponseServer.success(new PageInfo(collageModeService.getPageList(params)));
    }

    @GetMapping("/getAll")
    public ResponseServer getAll() {
        return ResponseServer.success(collageModeService.getAll());
    }

    @GetMapping("/getAllCollageModeByName")
    public ResponseServer getAllCollageModeByName() {
        return ResponseServer.success(collageModeService.getAllCollageModeByName());
    }

    @GetMapping("/getById/{id}")
    public ResponseServer getById(@PathVariable Integer id) {
        return ResponseServer.success(collageModeService.getById(id));
    }

}
